package com.example.myapplication.ad

import android.app.Activity
import android.util.Log
import android.widget.Toast
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTAdLoadType
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd
import com.example.myapplication.network.LaunchInfo

/**
 * 插屏广告管理
 */
class InterstitialAdManager(private val context: Activity) : AdManager() {
    // 插全屏广告对象
    private var mTTFullScreenVideoAd: TTFullScreenVideoAd? = null

    // 广告加载监听器
    private var mFullScreenVideoListener: TTAdNative.FullScreenVideoAdListener? = null

    // 广告展示监听器
    private var mFullScreenVideoAdInteractionListener: TTFullScreenVideoAd.FullScreenVideoAdInteractionListener? = null

    override fun loadAd(locationId: String?, count: Int, shouldShow: Boolean, isFullScreen: Boolean) {
        this.locationId = locationId
        if (locationId.isNullOrEmpty()) return
        //if (!needShow(locationId)) return

        val ad: LaunchInfo.AdSlot? = getCodeId(locationId)

        /** 1、创建AdSlot对象  */
        val adslot = AdSlot.Builder()
            .setCodeId(ad?.stubId)
            .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)
            .setAdLoadType(TTAdLoadType.PRELOAD)
            .build()


        /** 2、创建TTAdNative对象  */
        val adNativeLoader: TTAdNative? = TTAdManagerHolder.get()?.createAdNative(context)

        /** 3、创建加载、展示监听器  */
        initListeners(shouldShow)

        /** 4、加载广告  */
        adNativeLoader?.loadFullScreenVideoAd(adslot, this.mFullScreenVideoListener)
    }

    override fun showAd() {
        if (this.mTTFullScreenVideoAd == null) {
            Log.d(TAG, "请先加载广告或等待广告加载完毕后再调用show方法")
            return
        }
        /** 5、设置展示监听器，展示广告  */
        this.mTTFullScreenVideoAd.apply {
            this!!.setFullScreenVideoAdInteractionListener(mFullScreenVideoAdInteractionListener)
            showFullScreenVideoAd(context)
        }
        this.mTTFullScreenVideoAd = null
    }

    override fun initListeners(shouldShow: Boolean) {
        // 广告加载监听器
        this.mFullScreenVideoListener = object : TTAdNative.FullScreenVideoAdListener {
            override fun onError(code: Int, message: String?) {
                Toast.makeText(context,"us:${locationId} code: = $code --- msg: = $message",Toast.LENGTH_SHORT).show()
                Log.d(TAG, "InterstitialFull onError code = $code msg = $message")
            }

            override fun onFullScreenVideoAdLoad(ad: TTFullScreenVideoAd?) {
                Log.d(TAG, "InterstitialFull onFullScreenVideoLoaded")
                mTTFullScreenVideoAd = ad
            }

            override fun onFullScreenVideoCached() {
                Log.d(TAG, "InterstitialFull onFullScreenVideoCached")
                if (shouldShow) {
                    showAd()
                }
            }

            override fun onFullScreenVideoCached(ad: TTFullScreenVideoAd?) {
                Log.d(TAG, "InterstitialFull onFullScreenVideoCached")
                mTTFullScreenVideoAd = ad
            }
        }
        // 广告展示监听器
        this.mFullScreenVideoAdInteractionListener = object : TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {
            override fun onAdShow() {
                preShowTime[locationId!!] = System.currentTimeMillis()
                Log.d(TAG, "InterstitialFull onAdShow")
            }

            override fun onAdVideoBarClick() {
                Log.d(TAG, "InterstitialFull onAdVideoBarClick")
            }

            override fun onAdClose() {
                Log.d(TAG, "InterstitialFull onAdClose")
            }

            override fun onVideoComplete() {
                Log.d(TAG, "InterstitialFull onVideoComplete")
            }

            override fun onSkippedVideo() {
                Log.d(TAG, "InterstitialFull onSkippedVideo")
            }
        }
    }

    /**
     * 6、在onDestroy中销毁广告
     *
     */
    override fun destroyAD() {
        mTTFullScreenVideoAd?.mediationManager?.destroy()
    }

}
